//
//  GGHomeTopView.m
//  Gunterapple
//
//  Created by 热带雨林 on 2020/4/1.
//  Copyright © 2020 热带雨林. All rights reserved.
//

#import "GGHomeTopView.h"
#import "GGAddtion.h"
@implementation GGHomeTopView

- (instancetype)initWithFrame:(CGRect)frame{
    if(self  = [super initWithFrame:frame]){
        [self setUpUI];
    }
    return self;
}
- (void)setUpUI{
    self.backgroundColor = [UIColor gg_colorWithR:53 G:174 B:220 alpha:1];
    [self addButtonWithImageName:@"home_scan" withTitle:@"扫一扫" withTag:btnScan];
    [self addButtonWithImageName:@"home_card" withTitle:@"卡包" withTag:btnCard];
    [self addButtonWithImageName:@"home_pay" withTitle:@"支付" withTag:btnPay];
    [self addButtonWithImageName:@"home_xiu" withTitle:@"咻一咻" withTag:btnXiu];
}
- (void)addButtonWithImageName:(NSString *)imageName withTitle:(NSString *)title withTag:(HomeTopViewBtnType)tag{
    UIButton *button = [[UIButton alloc]init];
    //设置属性文字
    [button setAttributedTitle:[NSAttributedString creatBtnWithImage:[UIImage imageNamed:imageName] withImageWight:38 withImageHeight:38 withTitle:title wihtFontSize:14 withtitleColor:[UIColor whiteColor] spacing:7] forState:UIControlStateNormal];
    button.titleLabel.numberOfLines = 0;
    button.titleLabel.textAlignment = NSTextAlignmentCenter;
    //添加点击事件
    [button addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
    button.tag = tag;
    [self addSubview:button];
}
- (void)clickBtn:(UIButton *)sender{
    if([self.delegate respondsToSelector:@selector(clickBtnWithType:)]){
        [self.delegate clickBtnWithType:sender.tag];
    }
}
//被添加到父视图时会被调用
- (void)layoutSubviews{
    [super layoutSubviews];
//    CGFloat margin = (self.frame.size.width - self.subviews.firstObject.frame.size.width * 4) / 5;
//    for (NSInteger i = 0; i < self.subviews.count - 1; i ++) {
//        //此处是布局
//        //可以通过子控件获得当前的button
//        UIButton *btn = self.subviews[i];
//        UIButton *nextBtn = self.subviews[i+1];
//        if(i == 0){
//            [btn mas_makeConstraints:^(MASConstraintMaker *make) {
//                make.left.equalTo(self).offset(margin);
//                make.centerY.equalTo(self);
//            }];
//        }
//        [nextBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.left.equalTo(btn.mas_right).offset(margin);
//            make.centerY.equalTo(self);
//        }];
//    }
    // 实现masonry水平固定间隔方法
     /*
      fixedSpacing:视图间的间距
      leadSpacing:最左侧视图距离父视图左边距
      tailSpacing:最右侧视图距离父视图右边距
      AlongAxis:布局方式
      */
    // 1、水平方向排列、固定控件间隔、控件长度不定
//    [self.subviews mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:30 leadSpacing:5 tailSpacing:5];
    // 2、水平方向排列、固定控件长度、控件间隔不定
    [self.subviews mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedItemLength:100 leadSpacing:10 tailSpacing:10];
    //3、垂直方向排列
//    [self.subviews mas_distributeViewsAlongAxis:MASAxisTypeVertical withFixedItemLength:100 leadSpacing:10 tailSpacing:10];
    
    [self.subviews mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
        make.height.mas_equalTo(70);
    }];
}

@end
